package cn.jiangjiesheng.bootstrap.fileport.demo;

import cn.jiangjiesheng.bootstrap.fileport.infrastructure.utils.FileUtils;
import cn.jiangjiesheng.bootstrap.fileport.infrastructure.utils.JsonUtils;
import cn.jiangjiesheng.bootstrap.fileport.interfaces.DocExporter;
import cn.jiangjiesheng.bootstrap.fileport.interfaces.model.DocDataModel;
import lombok.var;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * Created by dev@jiangjiesheng.cn on 2021/3/22  <br/>
 * @author jerrylau
 */
@SpringBootTest(classes = DemoApplication.class)
public class DocExportExample {

    @Autowired
    private DocExporter docBuilder;

    @Test
    void buildTemporaryFile() throws FileNotFoundException {
        var tpl = FileUtils.readInputStreamFromResource("templates/templ.docx");
        var data = new DocDataModel()
                .setEChart("image", JsonUtils.toJsonNode(FileUtils.readStringFromResource("templates/testdata/pie.json")),400,300)
                .setText("title", "安全无忧-fileport模块测试")
                .setText("name","名称")
                .setImage("image1", ResourceUtils.getFile("classpath:templates/testdata/img.png"),720,360)
                .setTable("table", new String[][]{
                        new String[]{"名称", "年龄"},
                        new String[]{"小张", "11"},
                        new String[]{"小米", "23"},
                        new String[]{"小红", "98"},
                })
                .setText("article","北国风光，千里冰封，万里雪飘。望长城内外，惟余莽莽2；大河上下，顿失滔滔3。山舞银蛇，原驰蜡象4，欲与天公试比高。须晴日5，看红装素裹，分外妖娆6。江山如此多娇，引无数英雄竞折腰7。惜秦皇汉武，略输文采8；唐宗宋祖9，稍逊风骚。一代天骄，成吉思汗，只识弯弓射大雕。俱往矣，数风流人物，还看今朝。")
                .setImageFromUrl("image3","https://bkimg.cdn.bcebos.com/pic/6d81800a19d8bc3eb1355f6baac5b11ea8d3fd1fef76?x-bce-process=image/format,f_auto",200,400)
                ;
        File file = docBuilder.setTemplate(tpl).setDocDataModel(data).buildTemporaryFile();
        Assertions.assertTrue(file.isFile());
        System.out.println(file.getAbsoluteFile());
    }

    @Test
    void buildInputStream() {
        var tpl = FileUtils.readInputStreamFromResource("templates/templ.docx");
        var data = new DocDataModel()
                .setEChart("image", JsonUtils.toJsonNode(FileUtils.readStringFromResource("templates/testdata/pie.json")),400,300)
                .setText("title", "安元")
                .setText("name","名称")
                .setTable("table", new String[][]{
                        new String[]{"名称", "年龄"},
                        new String[]{"小张", "11"},
                        new String[]{"小米", "23"},
                        new String[]{"小红", "98"},
                })
                .setText("article","北国风光，千里冰封，万里雪飘。望长城内外，惟余莽莽2；大河上下，顿失滔滔3。山舞银蛇，原驰蜡象4，欲与天公试比高。须晴日5，看红装素裹，分外妖娆6。江山如此多娇，引无数英雄竞折腰7。惜秦皇汉武，略输文采8；唐宗宋祖9，稍逊风骚。一代天骄，成吉思汗，只识弯弓射大雕。俱往矣，数风流人物，还看今朝。");

        InputStream file = docBuilder.setTemplate(tpl).setDocDataModel(data).buildInputStream();
        System.out.println(file);
    }

    @Test
    void buildPdf() {
        var tpl = FileUtils.readInputStreamFromResource("templates/templ.docx");
        var data = new DocDataModel()
                .setEChart("image", JsonUtils.toJsonNode(FileUtils.readStringFromResource("templates/testdata/pie.json")),400,300)
                .setText("title", "安元")
                .setText("name","名称")
                .setTable("table", new String[][]{
                        new String[]{"名称", "年龄"},
                        new String[]{"小张", "11"},
                        new String[]{"小米", "23"},
                        new String[]{"小红", "98"},
                })
                .setText("article","北国风光，千里冰封，万里雪飘。望长城内外，惟余莽莽2；大河上下，顿失滔滔3。山舞银蛇，原驰蜡象4，欲与天公试比高。须晴日5，看红装素裹，分外妖娆6。江山如此多娇，引无数英雄竞折腰7。惜秦皇汉武，略输文采8；唐宗宋祖9，稍逊风骚。一代天骄，" +
                        "成吉思汗，只识弯弓射大雕。俱往矣，数风流人物，还看今朝。");
        File file = docBuilder.setTemplate(tpl).setDocDataModel(data).buildPdf();
        Assertions.assertTrue(file.isFile());
        System.out.println(file);
    }
}
